package cn.tedu.ivos.application.service.impl;

import cn.tedu.ivos.application.mapper.ApplicationMapper;
import cn.tedu.ivos.application.pojo.dto.ApplicationQueryParam;
import cn.tedu.ivos.application.pojo.dto.ApplicationSaveParam;
import cn.tedu.ivos.application.pojo.entity.Application;
import cn.tedu.ivos.application.pojo.vo.ApplicationVO;
import cn.tedu.ivos.application.service.ApplicationService;
import cn.tedu.ivos.audit.mapper.AuditMapper;
import cn.tedu.ivos.audit.pojo.vo.AuditVO;
import cn.tedu.ivos.audit.service.AuditService;
import cn.tedu.ivos.base.enums.ApplicationStatusEnum;
import cn.tedu.ivos.user.mapper.UserMapper;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.entity.Vehicle;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.StringJoiner;

@Slf4j
@Service
public class ApplicationServiceImpl implements ApplicationService {

    @Autowired
    ApplicationMapper applicationMapper;
    @Autowired
    AuditService auditService;
    @Autowired
    UserMapper userMapper;
    @Autowired
    AuditMapper auditMapper;
    @Autowired
    VehicleMapper vehicleMapper;
    @Override
    public void save(ApplicationSaveParam param) {
        log.debug("开始处理保存业务："+param);
        Application app=new Application();
        BeanUtils.copyProperties(param,app);
        // 设置申请单状态为"已发起"(10)
        app.setStatus(ApplicationStatusEnum.PENDIND.getCode());
        app.setCreateTime(new Date());
        /* 遇到的问题:新增申请单对应的审批单时,审批单数据没有此申请单id
        原因:执行insert方法的SQL时,并没有把刚刚生成的申请单id回填到application对象中
        解决办法:给此SQL上加useGeneratedKeys="true" keyProperty="id"属性
        效果:JDBC自动回填此申请单id到application对象的id属性中,再传给审批,审批就拿到申请单id了
        */
        applicationMapper.save(app);
        //还需要为当前申请生成对应的1到多条审批单数据
        auditService.insertAudit(app);
    }

    @Override
    public List<ApplicationVO> selectApplication(ApplicationQueryParam param) {
        log.debug("开始处理查询业务："+param);
        List<ApplicationVO>list=applicationMapper.selectApplication(param);
        //遍历得到的每一个申请单VO，为它补全审批人的数据
        //List<Long> auditUserIdList;//审批人id集合
        //String auditUsernameList;//审批人姓名字符串
        for (int i=0;i<list.size();i++){
            ApplicationVO app=list.get(i);
            //创建一个自定义方法，为当前循环到的这个申请单VO补全审批人数据
            assignAuditUserList(app);
        }
        return list;
    }

    @Override
    public void cancel(Long id) {
        log.debug("取消申请：id="+id);
        Application app=new Application();
        app.setId(id);
        app.setStatus(ApplicationStatusEnum.CANCEL.getCode());
        app.setUpdateTime(new Date());
        applicationMapper.cancel(app);

        auditMapper.deleteByApplicationId(id);

    }



    private void assignAuditUserList(ApplicationVO app) {
        //1 准备空集合来存放多个审批人id与姓名
        List<Long> auditUserIdList = new ArrayList<>();
        List<String> auditUsernameList = new ArrayList<>();
        //2. 根据申请单id ，查出审批此申请单的所有审批单
        List<AuditVO> auditVOList=auditService.selectAuditByApplicationId(app.getId());
        //3. 遍历依次取出每个审批单VO
        for (AuditVO auditVO : auditVOList) {
            Long id = auditVO.getAuditUserId();
            auditUserIdList.add(id);

            String username = userMapper.selectById(id).getUsername();
            auditUsernameList.add(username);
        }
        //6. 准备一个字符串拼接工具，帮我们把多个审批人姓名连成一个字符串
        StringJoiner joiner = new StringJoiner(",");
        for (String username : auditUsernameList){
            joiner.add(username);
        }
        //8.给传入的申请单VO补全数据
        app.setAuditUserIdList(auditUserIdList);
        app.setAuditUsernameList(joiner.toString());
    }

    @Override
    public void distribute(Long applicationId, Long vehicleId) {
        log.debug("开始处理分配业务：applicationId="+applicationId+",vehicleId="+vehicleId);
        Application app=new Application();
        app.setId(applicationId);
        app.setVehicleId(vehicleId);
        app.setStatus(ApplicationStatusEnum.ALLOCATION.getCode());
        app.setUpdateTime(new Date());
        applicationMapper.cancel(app);

        Vehicle vehicle=new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setStatus("2");
        vehicle.setUpdateTime(new Date());
        vehicleMapper.updateVehicle(vehicle);



    }

    @Override
    public void returnCar(Long id, Long vehicleId) {
        log.debug("开始处理还车业务：id="+id+",vehicleId="+vehicleId);
        Application app=new Application();
        app.setId(id);
        app.setStatus(ApplicationStatusEnum.END.getCode());
        app.setUpdateTime(new Date());
        app.setVehicleId(null);
        applicationMapper.back(app);

        Vehicle vehicle=new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setStatus("1");
        vehicle.setUpdateTime(new Date());
        vehicleMapper.updateVehicle(vehicle);
    }


}
